﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace GameBase.Form.Control
{
    public class MyGroupControl : MyControl
    {
        protected List<MyControl> listControls = new List<MyControl>();
        public virtual void Add(MyControl control)
        {
            control.ScreenScale = screenScale;
            control.BasePosition = position * screenScale + basePosition;
            control.Depth = depth - 0.001f;
            listControls.Add(control);
        }
        public void RemoveAll()
        {
            listControls.Clear();
        }
        public override Vector2 ScreenScale
        {
            get { return screenScale; }
            set
            {
                screenScale = value;
                UpdateChildPosition();
            }
        }
        public override Vector2 Position
        {
            get
            {
                return position;
            }
            set
            {
                base.Position = value;
                UpdateChildPosition();
            }
        }
        public override Vector2 BasePosition
        {
            get
            {
                return basePosition;
            }
            set
            {
                base.BasePosition = value;
                UpdateChildPosition();
            }
        }
        protected virtual void UpdateChildPosition()
        {
            foreach (MyControl control in listControls)
            {
                control.ScreenScale = screenScale;
                control.BasePosition = position * screenScale + basePosition;
            }
        }
        
        public override MyControl MouseInput(MouseState mouseState)
        {
            if (!isEnable || !isVisible)
            {
                return null;
            }
            if (isHover)
            {
                if (mouseState.LeftButton == ButtonState.Pressed)
                {

                    MousePressed(mouseState.X, mouseState.Y);
                }
                else
                {
                    MouseReleased(mouseState.X, mouseState.Y);
                }
            }
            return ControlMouseInput(mouseState);
        }
        public override void ControlUpdate(GameTime gameTime)
        {
            base.ControlUpdate(gameTime);
            foreach (MyControl control in listControls)
            {
                control.Update(gameTime);
            }
        }
        public override void Draw(GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            if (!isVisible)
                return;
            base.Draw(gameTime, spriteBatch);

            foreach (MyControl control in listControls)
            {
                control.Draw(gameTime, spriteBatch);
            }
        }
        public override MyControl ControlMouseInput(Microsoft.Xna.Framework.Input.MouseState mouseState)
        {
            base.ControlMouseInput(mouseState);
            MyControl focusedControl=null;
            foreach (MyControl control in listControls)
            {
                MyControl ctr = control.MouseInput(mouseState);
                if (ctr != null)
                {
                    focusedControl = ctr;
                }
            }
            if (focusedControl != null)
            {
                 return focusedControl;
            }
            if (isHover)
            {
                return this;
            }
            return null;
        }
        public override float Depth
        {
            get
            {
                return base.Depth;
            }
            set
            {
                base.Depth = value;
                foreach (MyControl control in listControls)
                {
                    control.Depth = depth - 0.001f;
                }
            }
        }
    }

}
